package com.mercury.game.service.impl;

import com.mercury.game.entity.Tree;
import com.mercury.game.mapper.TreeMapper;
import com.mercury.game.service.TreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.user.service.CenterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 童雅婷
 * @since 2021-11-23
 */
@Service
public class TreeServiceImpl extends ServiceImpl<TreeMapper, Tree> implements TreeService {

    @Autowired
    private CenterUserService userService;

    @Override
    public boolean createTree(Tree tree) {
        tree.setTreeWater(10).setDeleted(false).setTreeStatus(0).setTreeWater(0);
        int res = baseMapper.insert(tree);
        if(res < 1) return false;
        else return true;
    }

    @Override
    public Tree updateWater(String id, String type) {
        Tree tree = baseMapper.selectById(id);
        int water = 0;
        if(type.equals("read")) {
            water = tree.getUserWater() + 10;
        }else if(type.equals("commit")){
            water = tree.getUserWater() + 5;
        }else if(type.equals("share")) {
            water = tree.getUserWater() + 20;
        }else if(type.equals("login")){
            water = tree.getUserWater() + 10;
        }else if(type.equals("question")) {
            water = tree.getUserWater() + 20;
        }
        tree.setUserWater(water);
        baseMapper.updateById(tree);
        return tree;
    }

    /**
     * @date 2021/11/23 20:03
     * @params id
     * @return
     * @description 用户给树浇水，要是水量够了就升级，超过设置的就调用结果方法，返回结果的数量
    */
    @Override
    public Tree useWater(int id) {
        Tree tree = baseMapper.selectById(id);
        tree.setTreeWater(tree.getTreeWater() + 10);
        tree.setUserWater(tree.getUserWater() - 10);

        if(30 <= tree.getTreeWater() && tree.getTreeWater() < 60) {
            tree.setTreeStatus(1);
        }else if(tree.getTreeWater() >= 60 && tree.getTreeWater() <= 90) {
            tree.setTreeStatus(2);
        }else if(tree.getTreeWater() > 90) {
            tree.setTreeStatus(0);
        }
        baseMapper.updateById(tree);
        return tree;

    }

    @Override
    public int setUserBeans(int id) {
        int beanNums = userService.setUserBeans(baseMapper.selectById(id).getUserId());
        return beanNums;
    }


}
